﻿// Original author contact info: Owen Emlen (owene_1998@yahoo.com)
// Note: other individuals may also have contributed to this code
// Project hosted on CodePlex.com as of 1/10/2009 at http://wpfimageviewer.codeplex.com
using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Threading;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Threading;
using BitmapTagger;
using BrainTechLLC;
using BrainTechLLC.FilterExtensions;
using BrainTechLLC.ThreadSafeObjects;
using BrainTechLLC.WPF;
using ImageAppDataModels;
using System.Collections.Specialized;
using BrainTechLLC.ImageOperatorsNonAForgeDependent;
using System.Reflection;
using System.Collections.ObjectModel;

namespace ImageAppViewModels
{
    public class GroupCollectionViewModel : ObservableCollection<GroupViewModel>
    {
        public event EventHandler<ObjectEventArgs<GroupCollectionViewModel>> CurrentlySelectedGroupsInListboxChanged;
        public ThreadSafeLookup<string, GroupViewModel> GroupLookupsByName = new ThreadSafeLookup<string, GroupViewModel>();
        public List<FileImageGroup> AllFileImageGroups = new List<FileImageGroup>();
        protected List<GroupViewModel> _CurrentlySelectedGroupsInListbox = new List<GroupViewModel>();
        public List<FileImageGroupViewModel> AllFileImageGroupViewModels = new List<FileImageGroupViewModel>();
        protected bool _ShowOnlySelectedGroups;
        //public ThreadSafeListAndLookup<string, FileImageGroupViewModel> FileImageLookupByGroupName = new ThreadSafeListAndLookup<string, FileImageGroupViewModel>();

        public List<GroupViewModel> CurrentlySelectedGroupsInListbox
        {
            get { return _CurrentlySelectedGroupsInListbox; }
        }

        public GroupViewModel FindGroup(string groupName)
        {
            return GroupLookupsByName[groupName];
        }

        public GroupViewModel AddOrFindGroup(string groupName)
        {
            if (GroupLookupsByName.AddIfNew(groupName, () => new GroupViewModel(new Group() { GroupName = groupName }, this)))
            {
                base.Add(GroupLookupsByName[groupName]);
            }
            return GroupLookupsByName[groupName];
        }

        public FileImageGroupViewModel AddFileImageToGroup(string fileName, string groupName)
        {
            GroupViewModel group = AddOrFindGroup(groupName);
            var found = group.FindFindImage(fileName);
            if (found != null)
            {
                return found;
            }
            FileImageGroup fileImageGroup = new FileImageGroup() { FileName = fileName, GroupName = groupName };
            FileImageGroupViewModel vm = new FileImageGroupViewModel(fileImageGroup);
            AllFileImageGroups.Add(fileImageGroup);
            AllFileImageGroupViewModels.Add(vm);
            group.AddFileImageToGroup(vm);
            //FileImageLookupByGroupName.Add(groupName, vm);
            return vm;
        }

        public void RemoveFileImageFromGroup(string fileName, string groupName)
        {
            GroupViewModel group = AddOrFindGroup(groupName);
            var found = this.AllFileImageGroupViewModels.FirstOrDefault(item => item.FileName == fileName && item.GroupName == groupName);
            AllFileImageGroups.RemoveAll(item => item.FileName == fileName && item.GroupName == groupName);
            AllFileImageGroupViewModels.Remove(found);
            group.RemoveFileImageFromGroup(found);
        }

        public void RemoveFileImageFromGroup(FileImageGroupViewModel o)
        {
            GroupViewModel group = AddOrFindGroup(o.GroupName);
            AllFileImageGroups.RemoveAll(item => item.FileName == o.FileName && item.GroupName == o.GroupName);
            AllFileImageGroupViewModels.Remove(o);
            group.RemoveFileImageFromGroup(o);
        }

        public bool ShowOnlySelectedGroups
        {
            get { return _ShowOnlySelectedGroups; }
            set
            {
                if (_ShowOnlySelectedGroups != value)
                {
                    _ShowOnlySelectedGroups = value;                    
                }
            }
        }

        public List<GroupViewModel> GetSelectedGroups()
        {
            return this.Where(o => o.IsSelected).ToList();
        }

        public void RemoveGroup(GroupViewModel vm)
        {
            AllFileImageGroups.RemoveAll(item => item.GroupName == vm.GroupName);
            AllFileImageGroupViewModels.RemoveAll(o => o.GroupName == vm.GroupName);
            vm.ClearFileImageFromGroup();
            base.Remove(vm);
            GroupLookupsByName.Remove(vm.GroupName);
        }

        public void OnCurrentlySelectedGroupsInListboxChanged()
        {
            CurrentlySelectedGroupsInListboxChanged.RaiseEvent(this, this);
        }
    }
}
